package avicit.im.lantop.basic.libstructuredefine.action.service.api;

import avicit.im.lantop.basic.libstructuredefine.domain.LantopArchiveClassT;
import avicit.platform6.commons.utils.web.TreeNode;
import avicit.platform6.core.rest.msg.ResponseMsg;
import avicit.platform6.core.restclient.RestClient;
import avicit.platform6.core.restclient.RestClientUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LantopArchiveClassServiceApi {


    /**
     * 服务编码
     */
    private static final String SERVICE_CODE = "filebasicmanager";

    private static final String BASE_PATH = "/api/avicit/im/lantop/basic/libstructuredefine/action/rest/LantopArchiveRest";

    @Autowired
    private RestClient restClient;


    public int getParentNodeByParentId(String deptId, String orgId) {
        String url = BASE_PATH + "/getParentNodeByParentId";
        Map<String,Object> map = new HashMap<>();
        map.put("deptId",deptId);
        map.put("orgId",orgId);
        ResponseMsg<Integer> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Integer>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     *  获取顶层节点
     * @param deptId
     * @param orgId
     */
    public ResponseMsg saveRootLantopArchiveTabClassVByType(String deptId, String orgId) {
        String url = BASE_PATH + "/saveRootLantopArchiveTabClassVByType";
        Map<String,Object> map = new HashMap<>();
        map.put("deptId",deptId);
        map.put("orgId",orgId);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
        return responseMsg;
    }

    /**
     * 查找树节点
     * @param id
     * @param level
     * @param orgId
     * @return
     */
    public List<TreeNode> recurFindTreeNodesByParentId(String id, int level, String orgId) {
        String url = BASE_PATH + "/recurFindTreeNodesByParentId";
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("level",level);
        map.put("orgId",orgId);
        ResponseMsg<List<TreeNode>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<TreeNode>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    public List<Map<String, Object>> getPublicFieldInfo() {
        String url = BASE_PATH + "/getPublicFieldInfo";
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    public LantopArchiveClassT getLantopArchiveClassById(String id) {
        String url = BASE_PATH + "/getLantopArchiveClassById";
        ResponseMsg<LantopArchiveClassT> responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<LantopArchiveClassT>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * FIXME (获取模板文件夹下子节点数量)
     * @param id
     * @return
     */
    public int getCountByClassFloder(String id) {
        String url = BASE_PATH + "/getCountByClassFloder";
        ResponseMsg<Integer> responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<Integer>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * FIXME (删除树节点)
     *
     * @author 刘中华
     * @param nodeId
     * @return
     * @throws Exception
     *             ModelAndView
     * @throws
     */
    public ResponseMsg deleteLantopArchiveClassById(String nodeId) {
        String url = BASE_PATH + "/deleteLantopArchiveClassById";
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, nodeId, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
        return responseMsg;
    }

    /**
     * 插入组织
     * @param deptId
     * @return
     */
    public String getOrgIdByDeptId(String deptId) {
        String url = BASE_PATH + "/getOrgIdByDeptId";
        ResponseMsg<String> responseMsg = restClient.doPost(SERVICE_CODE, url, deptId, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 删除字段对应设置相关数据
     * @param listTab
     */
    public ResponseMsg deleteLantopMapSet(List<Map<String, Object>> listTab) {
        String url = BASE_PATH + "/deleteLantopMapSet";
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, listTab, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
        return responseMsg;
    }

    public ResponseMsg insertLantopArchiveClass(LantopArchiveClassT lantopArchiveClassNew) {
        String url = BASE_PATH + "/insertLantopArchiveClass";
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, lantopArchiveClassNew, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
        return responseMsg;
    }

    /**
     * 更新treePath
     * @param id
     * @return
     */
    public String getTreePath(String id) {
        String url = BASE_PATH + "/getTreePath";
        ResponseMsg<String> responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 更新treePath
     * @param lantopArchiveClassNew
     * @return
     */
    public void updateLantopArchiveClass(LantopArchiveClassT lantopArchiveClassNew) {
        String url = BASE_PATH + "/updateLantopArchiveClass";
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, lantopArchiveClassNew, new ParameterizedTypeReference<ResponseMsg<T>>() {
        });
    }

    public List<Map<String, Object>> getReturnAttribute(String id) {
        String url = BASE_PATH + "/getReturnAttribute";
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, id, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 验证当前组织是否创建全宗信息
     * @param orgId
     * @param sysId
     * @return
     */
    public boolean checkCurrentFonds(String orgId, String sysId) {
        String url = BASE_PATH + "/checkCurrentFonds";
        Map<String,Object> map = new HashMap<>();
        map.put("orgId",orgId);
        map.put("sysId",sysId);
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, sysId, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 验证同级别的节点名称是否已经存在
     * @param lantopArchiveClass
     * @return
     */
    public boolean checkExtName(LantopArchiveClassT lantopArchiveClass) {
        String url = BASE_PATH + "/checkExtName";
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, lantopArchiveClass, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }
}
